﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using Microsoft.Forums.MicrosoftForumsProvider;
using Microsoft.Forums.SyncService;
using Microsoft.Forums.OfflineStore;
using Microsoft.Forums.ForumsClient.Controllers;
using Microsoft.Forums.ForumsClient.Models;

namespace Microsoft.Forums.ForumsClient
{
    /// <summary>
    /// Interaction logic for SubscribeToForums.xaml
    /// </summary>
    public partial class SubscribeToForumsDialog : Window
    {

        SubscribeToForumsDataModel subscribeDataModel;
        SubscribeToForumsController controller;


        /// <summary>
        ///  Subscription Dialog 
        /// </summary>
        public SubscribeToForumsDialog(SubscribeToForumsDataModel subscribeModel)
        {
            if (subscribeModel == null) throw new ArgumentNullException("subscribeModel", "subscribeModel cannot be null");

            InitializeComponent();
            subscribeDataModel = subscribeModel;
            controller = new SubscribeToForumsController(subscribeDataModel);
            this.DataContext = subscribeDataModel;
            subscribeDataModel.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(SubscribeDataModel_PropertyChanged);
            this.Cursor = Cursors.Wait;

            controller.InitializeData();

        }




        /// <summary>
        /// Handles the PropertyChanged event of the SubscribeDataModel control.
        /// We are only interested in when IsDoneLoading changes so we can update the cursor
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.ComponentModel.PropertyChangedEventArgs"/> instance containing the event data.</param>
        void SubscribeDataModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName.Equals("IsDoneLoading", StringComparison.Ordinal))
            {
                if (subscribeDataModel.IsDoneLoading)
                {
                    this.Cursor = Cursors.Arrow;
                }
            }
        }

        


        #region Close Command Handlers
        private void Close_Executed(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            this.Close();
        }
        #endregion


        #region SubscribeToForums Command Handlers
        private void SubscribeToForums_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = controller.CanSubscribeToForums;
        }

        private void SubscribeToForums_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            controller.SubscribeToForums();
            DialogResult = true;
            this.Close();
        }
        #endregion


        #region RefreshAvailableForums Command Handlers
        private void RefreshAvailableForums_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = controller.CanRefreshAvailableForums;
        }

        private void RefreshAvailableForums_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            controller.RefreshAvailableForums();
        }
        #endregion

    }
}
